package org.stvd.service.admin.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.RoleMenu;
import org.stvd.entities.admin.Roles;
import org.stvd.entities.admin.UserRole;
import org.stvd.repository.admin.RoleMenuDao;
import org.stvd.repository.admin.RoleResDao;
import org.stvd.repository.admin.RolesDao;
import org.stvd.repository.admin.UserRoleDao;
import org.stvd.service.admin.RolesService;
import org.stvd.service.base.impl.BaseServiceImpl;

@Service("rolesService")
public class RolesServiceImpl extends BaseServiceImpl<Roles> implements RolesService {

    @Resource(name = "RolesDao")
    private RolesDao rolesDao;
    @Resource(name = "RoleMenuDao")
    private RoleMenuDao roleMenuDao;
    @Resource(name = "RoleResDao")
    private RoleResDao roleResDao;
    @Resource(name = "UserRoleDao")
    private UserRoleDao userRoleDao;

    @Override
    public List<Map<String, Object>> listUserRolesByGuid(String guid) {
        return rolesDao.listUserRolesByGuid(guid);
    }
    
    @Override
    public QueryResult<Roles> getRoleQueryResult(int start, int limit, String depId, String guid) {
        return rolesDao.getRoleQueryResult(start, limit, depId, guid);
    }

    @Override
    public List<Roles> getRolesByResId(String resId) {
        return rolesDao.findRolesByResId(resId);
    }

    @Override
    public List<Roles> getRolesByUserId(String userId, String guid) {
        return rolesDao.findRolesByUserId(userId, guid);
    }

    public List<RoleMenu> findRoleMenuByRoleId(String roleId, String guid) {
        return roleMenuDao.findRoleMenuByRid(roleId, guid);
    }


    @Override
    public List<Roles> findRolesByDepId(String depId, String guid) {
        return rolesDao.findRolesByDepId(depId, guid);
    }

    public List<Roles> findValidRolesByDepId(String depId, String guid) {
        return rolesDao.findValidRolesByDepId(depId, guid);
    }
    
    @Transactional
    @CacheEvict(value = "adminCache", allEntries = true)
    public void insertRoleMenu(String roleId, String[] menuIds, String guid) {
        roleMenuDao.insertRoleMenu(roleId, menuIds, guid);
    }
    
    @Transactional
    public void insertRoleRes(String roleId, String[] resIds, String guid) {
        roleResDao.insertRoleRes(roleId, resIds, guid);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adminCache", allEntries = true)
    public void deleteRole(Object roleId, String guid) throws Exception {
        if (userRoleDao.findUserRoleByRoleid((String) roleId, guid).size() > 0) {
            throw new Exception("该角色存在使用的用户，不能删除！");
        } else {
            rolesDao.delete(Roles.class, roleId);
        }
    }

    @Override
    public void insertUserRole(String userId, String roleCode) {
        List<Map<String, Object>> list = rolesDao.listUserNoneRoleGuid(userId, roleCode);
        if(list!=null && list.size() > 0) {
            UserRole userRole = null;
            for (Map<String, Object> dataMap : list) {
                userRole = new UserRole();
                userRole.setId(StringUtil.getRandomUUID());
                userRole.setUserId(userId);
                userRole.setRoleId((String)dataMap.get("role_id"));
                userRole.setGuid((String)dataMap.get("guid"));
                userRoleDao.insert(userRole);
                userRole = null;
            }
        }
    }
    
    @Override
    public void insertUserRole(String userId, String roleCode, String guid) {
        Roles roles = rolesDao.getRolesByRoleCode(roleCode, guid);
        if(roles!=null) {
            UserRole userRole = this.getUserRoleByRoleCode(userId, roleCode, guid);
            if(userRole==null) {
                userRole = new UserRole();
                userRole.setId(StringUtil.getRandomUUID());
            }
            userRole.setUserId(userId);
            userRole.setRoleId(roles.getRoleId());
            userRole.setGuid(guid);
            userRoleDao.update(userRole);
        }
    }
    
    @Override
    public void deleteUserRole(String userId, String roleCode) {
        rolesDao.deleteUserRoleGuid(userId, roleCode);
    }
    
    @Override
    public void deleteUserRole(String userId, String roleCode, String guid) {
        rolesDao.deleteUserRoleGuid(userId, roleCode, guid);
    }

    @Override
    public Roles getRolesByRoleCode(String roleCode, String guid) {
        return rolesDao.getRolesByRoleCode(roleCode, guid);
    }
    
    @Override
    public UserRole getUserRoleByRoleCode(String userId, String roleCode, String guid) {
        return userRoleDao.getUserRoleByUesrRoleCode(userId, roleCode, guid);
    }
}
